<?php
class Store_model extends CI_Model {

  public function __construct()
  {
    $this->load->database();
  }

  public function CheckUserName($username)
  {
    $query = $this->db->query("select * from admin where id = '" . $username . "'");
    $query = $query->result_array();
    if (null==$query)
    {
      return false;
    } 
    else 
    {
      return true;
    }
  }

  public function CheckPassword($username,$pwd)
  {
    $query = $this->db->query("select * from admin where id = '" . $username . "'");
    $query = $query->row_array();
    if (null!=$query && $query['pwd'] == $pwd)
    {
      return true;
    }
    else
    {
      return false;
    }
  }

  public function get_stores()
  {
    $query = $this->db->get('store');
    return $query->result_array();
  }

  public function get_store_mail($store_id)
  {
    $query = $this->db->query("select store_mail from store where store_id=" . $store_id);

    if ($query->num_rows() > 0)
    {
       $row = $query->row(); 

       return $row->store_mail;
    }
    return "invalidstoredi@error.com";
  }

  public function get_avastores() //get avaiable stores
  {
    $this->db->where('store_state', 1);
    $query = $this->db->get('store');
    return $query->result_array();
  }  

  public function get_show_stores() //得到给用户展示的店铺
  {
    $query = $this->db->query("select * from store where store_state=1 or store_state=3 or store_state=4 order by store_pri desc");
    return $query->result_array();
  }


  public function get_store($store_id)
  {
    if ($store_id == '')
      return;

    $this->db->where('store_id', $store_id);
    $query = $this->db->get('store');
    return $query->result_array();
  }

  public function new_store($pic_address)
  {
    $data = array(
      'store_name' => $this->input->post('store_name'),
      'store_pic' => $pic_address,
      'store_note' => $this->input->post('store_note'),
      'store_pri' => $this->input->post('store_pri'),
      'store_mail' => $this->input->post('store_mail'),
      'store_beiwang' => $this->input->post('store_beiwang'),
      'store_printer_IMEI' => $this->input->post('store_printer_IMEI'),
      'store_state' => 1,
      'store_bottom_price' => $this->input->post('store_bottom_price'),
      'store_open_time' => $this->input->post('store_open_time'),
      'store_close_time' => $this->input->post('store_close_time'),
    );
    $this->db->insert('store', $data);
    return "添加成功";
  }

  public function update_store($pic_address)
  {
    $changeId = $this->input->get('store_id', TRUE);
    if ($changeId == '')
      return; 

    $data = array();
    if ($pic_address == '')
    {
      $data = array(
        'store_name' => $this->input->post('store_name'),
        'store_note' => $this->input->post('store_note'),
        'store_mail' => $this->input->post('store_mail'),
        'store_pri' => $this->input->post('store_pri'),
        'store_beiwang' => $this->input->post('store_beiwang'),
        'store_printer_IMEI' => $this->input->post('store_printer_IMEI'),
        'store_bottom_price' => $this->input->post('store_bottom_price'),
        'store_open_time' => $this->input->post('store_open_time'),
        'store_close_time' => $this->input->post('store_close_time'),
      );
    }
    else
    {
      $data = array(
        'store_name' => $this->input->post('store_name'),
        'store_pic' => $pic_address,
        'store_note' => $this->input->post('store_note'),
        'store_mail' => $this->input->post('store_mail'),
        'store_printer_IMEI' => $this->input->post('store_printer_IMEI'),
        'store_bottom_price' => $this->input->post('store_bottom_price'),
        'store_open_time' => $this->input->post('store_open_time'),
        'store_close_time' => $this->input->post('store_close_time'),
      );
    }  

    $this->db->where('store_id', $changeId);
    $this->db->update('store', $data);
    return "更新成功";
  }

  public function delete_store()
  {
    $deleteId = $this->input->get('store_id', TRUE);
    if ($deleteId == '')
      return;
      
    $data = array(
      'store_state' => 2,
    );
    $this->db->where('store_id', $deleteId);
    return $this->db->update('store', $data);
  } 

  public function back_store()
  {
    $deleteId = $this->input->get('store_id', TRUE);
    if ($deleteId == '')
      return;
      
    $data = array(
      'store_state' => 1,
    );
    $this->db->where('store_id', $deleteId);
    return $this->db->update('store', $data);
  } 

  public function close_store()
  {
    $closeId = $this->input->get('store_id', TRUE);
    if ($closeId == '')
      return;
      

    $data = array(
      'store_state' => 3,
    );
    $this->db->where('store_id', $closeId);
    return $this->db->update('store', $data);
  } 

  public function busy_store()
  {
    $closeId = $this->input->get('store_id', TRUE);
    if ($closeId == '')
      return;
      

    $data = array(
      'store_state' => 4, // store is busy with 4
    );
    $this->db->where('store_id', $closeId);
    return $this->db->update('store', $data);
  } 

  public function get_store_id($IMEI)
  {
    $query = $this->db->query("select store_id from store where length(store_printer_IMEI) > 0 and store_printer_IMEI = '" . $IMEI . "'");
    $store_id = $query->row_array();
    return $store_id;
  }

  public function get_store_bottom_price($store_id)
  {
    $query = $this->db->query("select store_bottom_price from store where store_id = '" . $store_id . "'");
    $store_bottom_price = $query->row_array();
    return $store_bottom_price;
  }

  public function get_shop_hours($store_id)//获得营业时间
  {
    $query = $this->db->query("select store_open_time,store_close_time from store where store_id= '" . $store_id . "'");
    $shop_hours = $query->row_array();
    return $shop_hours;
  }

  public function insertPrinterLog($printerUrl, $printerReturnContent, $printer_store_id, $IMEI)//插入打印机日志信息
  {
    $data = array(
      'printerUrl' => $printerUrl,
      'printerReturnContent' => $printerReturnContent,
      'printer_store_id' => $printer_store_id,
      'IMEI' => $IMEI,
    );
    $this->db->insert('printer_log', $data);
    return "添加成功";
  }



  }